#!/user/bin/env node

const Koa = require('koa');
const app = new Koa();
const log = console.log;

let cost;

app.use(async (ctx,next)=>{  
  next();
  //const cost = ctx.response.get('cost');//响应头方式得到logger中间件传回来的数据

  //const cost = ctx.cost;  //ctx方式传递
  log(`${ctx.method}${ctx.path}-${cost}ms`)
});

app.use((ctx,next)=>{
  const start = Date.now();//统计M-hello执行时间
  next();
  const end = Date.now();
  ctx.set('cost',end-start);   //将数据传递给上一个中间件
  ctx.cost = end-start;  //ctx方式传递
  return cost = end-start; //return 方式
});

app.use((ctx,next)=>{
  ctx.body = 'hello world';
});

app.listen(8080);
